package com.xiong.controlor;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xiong.client.Xsclient;
import com.xiong.poje.Xs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@DefaultProperties(defaultFallback = "test")   //将该控制器所有正常的请求都调用默认的降级逻辑
public class Controlor {

    @Autowired
    private Xsclient xsclient;

    @GetMapping("/ask")
    public List<Xs> getall(){
        List<Xs> list=xsclient.getall();

        return list;
    }


//开启服务调用这个服务之前的写法
/*    @Autowired
    private RestTemplate template;
    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/ask/{id}")
    //开启服务熔断，给服务配置处理降级的逻辑
 //   @HystrixCommand(fallbackMethod = "test")
    @HystrixCommand
    public String getall(@PathVariable int id){
        String url="";
        //直接拿到服务
*//*        List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances("service");
        ServiceInstance serviceInstance = serviceInstanceList.get(0);
        url = "http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/test1";*//*

        //负载均衡拿到服务

        if (id==1){
            throw new RuntimeException("超时");
        }

        url="http://service/test1";
        String json=template.getForObject(url,String.class);
        return  json;
        }*/

    public String test(){
        String massage="服务超时，请稍等------";
        return massage;
    }

}
